package com.henu.xyh;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.henu.R;
import com.henu.adapter.Listexpresswadapter;
import com.henu.xyh.entity.Express;
import com.henu.xyh.entity.Reply;
import com.henu.yizhiya.entity.Person;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.UpdateListener;

public class MyexpressActivity extends AppCompatActivity {

    private ImageButton myexpress_return;
    private Listexpresswadapter adapter;
    private ListView lv_myExpress;
    private ArrayList<Express> list_ex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myexpress);
        myexpress_return= (ImageButton) findViewById(R.id.myexpress_return);
        myexpress_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        adapter = new Listexpresswadapter(this);
        lv_myExpress = (ListView) findViewById(R.id.lv_myExpress);
        lv_myExpress.setAdapter(adapter);


        Person person = BmobUser.getCurrentUser(Person.class);
        String p_id = person.getObjectId();

        //显示自己发表的信息
        BmobQuery<Express> query = new BmobQuery<>();
        query.addWhereEqualTo("User_Id",p_id);
        query.order("-createdAt");
        query.findObjects(new FindListener<Express>() {
            @Override
            public void done(List<Express> list, BmobException e) {
                if(e==null){
                    list_ex=(ArrayList)list;
                    adapter.addData(list_ex); // 绑定数据
                    adapter.notifyDataSetChanged();// 显示
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });

        //展示表白墙回复
        lv_myExpress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MyexpressActivity.this,Express_ReplyActivity.class);
                Express express=list_ex.get(position);
                intent.putExtra("listview",express);
                startActivity(intent);
            }
        });
        //删除某一条表白信息
        lv_myExpress.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                final Express express = list_ex.get(position);

                BmobQuery<Reply> query = new BmobQuery<>();
                query.addWhereEqualTo("Express_Id",express.getObjectId());
                query.setLimit(1000);
                query.findObjects(new FindListener<Reply>() {
                    @Override
                    public void done(List<Reply> list, BmobException e) {
                        if (e==null){
                            //如果没有回复
                            if(list.size()==0){
                                express.delete(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e == null) {
                                            list_ex.remove(express);
                                            adapter.addData(list_ex); // 绑定数据
                                            adapter.notifyDataSetChanged();// 显示
                                            Toast.makeText(MyexpressActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(MyexpressActivity.this, "失败express" + e.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }else{
                                //存在回复
                                List<BmobObject> list_r = new ArrayList<BmobObject>();
                                for (Reply r:list) {
                                    list_r.add(r);
                                }
                                new BmobBatch().deleteBatch(list_r).doBatch(new QueryListListener<BatchResult>() {
                                    @Override
                                    public void done(List<BatchResult> list, BmobException e) {
                                        if (e==null){
                                            express.delete(new UpdateListener() {
                                                @Override
                                                public void done(BmobException e) {
                                                    if (e == null) {
                                                        list_ex.remove(express);
                                                        adapter.addData(list_ex); // 绑定数据
                                                        adapter.notifyDataSetChanged();// 显示
                                                        Toast.makeText(MyexpressActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                                    } else {
                                                        Toast.makeText(MyexpressActivity.this, "失败express" + e.getMessage(), Toast.LENGTH_SHORT).show();
                                                    }
                                                }
                                            });
                                        }else {
                                            Toast.makeText(MyexpressActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        }else {
                            Toast.makeText(MyexpressActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                return true;
            }
        });
    }
}
